package com.zzyk.main.model.pojo;

import com.zzyk.main.utils.SpringUtils;
import org.springframework.util.StringUtils;

import java.io.Serializable;

public class PageView implements Serializable {
    public static final long serialVersionUID = 1L;

    private static final String ASC = "ASC";
    private static final String DESC = "DESC";
    private static final String PAGE_INDEX = "pageIndex";
    private static final String PAGE_SIZE = "pageSize";
    private static final String ORDER_BY = "orderBy";
    private static final String IS_ASC = "isAsc";

    private Integer pageIndex;
    private Integer pageSize;
    private String orderBy;
    private Boolean isAsc;

    public Integer getPageIndex() {
        return pageIndex;
    }

    public void setPageIndex(Integer pageIndex) {
        this.pageIndex = pageIndex;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public String getOrderBy() {
        if (StringUtils.isEmpty(this.orderBy)) {
            return "";
        }
        return this.orderBy + " " + (isAsc ? ASC : DESC);
    }

    public void setOrderBy(String orderBy) {
        this.orderBy = orderBy;
    }

    public Boolean getAsc() {
        return isAsc;
    }

    public void setAsc(Boolean asc) {
        isAsc = asc;
    }

    public static PageView getPageView() {
        PageView pageView = new PageView();
        if (!StringUtils.isEmpty(SpringUtils.getParameter(PAGE_INDEX))) {
            pageView.setPageIndex(Integer.parseInt(SpringUtils.getParameter(PAGE_INDEX)));
        }
        if (!StringUtils.isEmpty(SpringUtils.getParameter(PAGE_SIZE))) {
            pageView.setPageSize(Integer.parseInt(SpringUtils.getParameter(PAGE_SIZE)));
        }
        pageView.setOrderBy(SpringUtils.getParameter(ORDER_BY));
        if (!StringUtils.isEmpty(SpringUtils.getParameter(IS_ASC))) {
            pageView.setAsc(Boolean.parseBoolean(SpringUtils.getParameter(IS_ASC)));
        }
        return pageView;
    }

    @Override
    public String toString() {
        return "PageView{" +
                "pageIndex=" + pageIndex +
                ", pageSize=" + pageSize +
                ", orderBy='" + orderBy + '\'' +
                ", isAsc=" + isAsc +
                '}';
    }
}
